/**
 * ****************************************************************************
 * Implementation of Common Utility.
 *
 * @author Ouyang Qi
 * @version 1.0.0 2008/11/14 (Standardized)
 * <PRE>
 * [Revision History]
 * 2008/09/26    ver1.0.0    Creation        Ouyang Qi
 * 2009/01/14    ver1.0.1    Modified        Ouyang Qi
 *      in method showPopDialog add main BAM id parameter
 *      add method parseDate()
 * </PRE>
 * Copyright 2008 USI, All rights reserved.
 * ***************************************************************************
 */

/**
 * @addon 
 */
Number.prototype.NaN0 = function(){return isNaN(this)?0:this;};

/**
 * 
 * @namespace Utility holds shortcut or handy functions.
 */
var Utility = {

	/**
	 * YUI has same feature. YAHOO.lang.substitute;
	 * 
	 * format function
	 * use array:
	 * format("This {1} is {0}",["beautiful","pen"]);
	 * =>  This pen is beautiful
	 *
	 * use hash:
	 * format("from {begin} to {end}",{begin : "today", end : 'next week'});
	 * 
	 * @methodOf Utility
	 * 
	 */
	format : function(f ,data){
		var s = data;
		var rep = function(a,b){
			var r = s[b];
			return typeof r == "undefined" ? a : r;
		};
		return f.replace(/{([^{}]*)}/g,rep);
	},
	
	/**
	* prevent from duplicatedly clicking
	*/
    unDuplicateFunction : function(func,object) {
	    var __method = func;
	    object = object || window;
	    args = $A(arguments);
	    args.shift();
	    args.shift();
	    
	    var wrapper = function(){
	    	var now = (new Date()).getTime();
	    	if(__method.lastTime && now - __method.lastTime < 2000) return;
	    	__method.lastTime = now;
	      	var ret = __method.apply(object, args.concat($A(arguments)));
	      	if(ret == "ret.cancel"){
	      		Utility.resetLastTimeForUnDuplicateFunction(__method);
	      	}
	    };
	    wrapper.__method=__method;
	    return wrapper;
    	
    } ,
    /**
    * reset last runned time for duplicatedly clicking check
    */
    resetLastTimeForUnDuplicateFunction : function(func) {
    	var __method = func.__method || func;
    	if(__method.lastTime) delete __method.lastTime;
    }
	
};